package com.nanyang.tms.manager.waybill.shipment.handler;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.shipment.ShipmentBillDao;
import com.nanyang.tms.dao.supplier.SupplierDao;
import com.nanyang.tms.dao.waybill.WaybillDao;
import com.nanyang.tms.dao.waybill.WaybillShipmentDao;
import com.nanyang.tms.dao.waybill.WaybillShipmentVolumeDao;
import com.nanyang.tms.domain.shipment.ShipmentBillVO;
import com.nanyang.tms.domain.supplier.SupplierProductVO;
import com.nanyang.tms.domain.supplier.SupplierVO;
import com.nanyang.tms.domain.waybill.WaybillDO;
import com.nanyang.tms.domain.waybill.WaybillShipmentDO;
import com.nanyang.tms.domain.waybill.WaybillShipmentVolumeDO;
import com.nanyang.tms.domain.waybill.WaybillVO;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightContext;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightVolumeContext;
import com.nanyang.tms.domain.waybill.shipment.context.CheckSupplierNoContext;
import com.nanyang.tms.domain.waybill.shipment.context.SaveWaybillShipmentContext;

@Component
public class SaveWaybillShipmentHandler extends AbstractTransHandler {
    @Autowired
	private WaybillDao waybillDao;
    @Autowired
    private WaybillShipmentVolumeDao waybillShipmentVolumeDao;
    @Autowired
    private WaybillShipmentDao waybillShipmentDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Autowired
	private SupplierDao  supplierDao;
	@Autowired
	private ShipmentBillDao  shipmentBillDao;
	@Override
	public boolean isAsync() {
		return false;
	}
	@SystemMessage(messageType=SystemMessage.MessageType.SERVICE,template="shipmentAddMsg")
	@SystemLog(module =SystemLog.Module.SHIPMENT,template="shipmentAddLog")
	@Override
	public boolean handle(TransHandlerContext context) {
		SaveWaybillShipmentContext saveWaybillShipmentContext=	(SaveWaybillShipmentContext) context.getInnerContext(SaveWaybillShipmentContext.class);
		CheckSupplierNoContext checkSupplierNoContext=	(CheckSupplierNoContext) context.getInnerContext(CheckSupplierNoContext.class);
		CalculateSupplierWeightContext  calculateSupplierWeightContext=(CalculateSupplierWeightContext) context.getInnerContext(CalculateSupplierWeightContext.class);
		try {
			ShipmentBillVO shipmentBillVO = shipmentBillDao.getShipmentBillByCode(saveWaybillShipmentContext.getShipmentCode()); //只查询了address 和operator
			if(ObjectUtils.isEmpty(shipmentBillVO)){
				throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(), CommonEnum.NO_CONTENT.getMessage());
			}
			String nextSq=TmsSerializeConstant.WAYBILL_SHIPMENT_KEY + globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.WAYBILL_SHIPMENT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
			WaybillVO waybillVO = waybillDao.selectBySupplierNo(checkSupplierNoContext.getSupplierNo());
			SupplierProductVO supplierProductVO = supplierDao.getSupplierByProductCode(calculateSupplierWeightContext.getProductCode());
			waybillDao.update(bulidWaybill(waybillVO));
			waybillShipmentDao.insert(bulidWaybillShipment(nextSq,shipmentBillVO, waybillVO, supplierProductVO, checkSupplierNoContext, calculateSupplierWeightContext, saveWaybillShipmentContext));
			int i=1;
			for(CalculateSupplierWeightVolumeContext calculateSupplierWeightVolumeContext:calculateSupplierWeightContext.getCalculateSupplierWeightVolumeContext()){
				waybillShipmentVolumeDao.insert(bulidWaybillShipmentVolume(waybillVO, i, calculateSupplierWeightVolumeContext, saveWaybillShipmentContext));
			}
			saveWaybillShipmentContext.setTotalNum(waybillVO.getTotalNum());
			saveWaybillShipmentContext.setWaybillCode(waybillVO.getWaybillCode());
			saveWaybillShipmentContext.setSupplierCode(supplierProductVO.getSupplierCode());
			saveWaybillShipmentContext.setAddress(shipmentBillVO.getAddress());
			saveWaybillShipmentContext.setCompanyCode(waybillVO.getCompanyCode());
			saveWaybillShipmentContext.setSupplierNo(checkSupplierNoContext.getSupplierNo());
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		
		
		return true;
	}
	
	private WaybillDO bulidWaybill(WaybillVO waybillVO){
	return 	WaybillDO.builder()
		.waybillCode(waybillVO.getWaybillCode())
	    .waybillType(1)
	    .build();
	}
	
	private WaybillShipmentDO bulidWaybillShipment (String nextSq, ShipmentBillVO shipmentBillVO,WaybillVO waybillVO,SupplierProductVO supplierProductVO,CheckSupplierNoContext checkSupplierNoContext,CalculateSupplierWeightContext  calculateSupplierWeightContext,SaveWaybillShipmentContext saveWaybillShipmentContext){
		WaybillShipmentDO build = WaybillShipmentDO.builder()
		.waybillShipmentCode(nextSq)
		.waybillCode(waybillVO.getWaybillCode())
		.companyCode(waybillVO.getCompanyCode())
		.companyName(waybillVO.getCompanyName())
		.totalChargeWeight(calculateSupplierWeightContext.getTotalChargeWeight())
		.totalWeight(calculateSupplierWeightContext.getTotalWeight())
		.totalVolumeWeight(calculateSupplierWeightContext.getTotalVolumeWeight())
		.cargoTypeCode(calculateSupplierWeightContext.getCargoTypeCode())
		.cargoTypeName(calculateSupplierWeightContext.getCargoTypeName())
		.productCode(supplierProductVO.getProductCode())
		.productName(supplierProductVO.getProductName())
		.supplierCode(supplierProductVO.getSupplierCode())
		.supplierName(supplierProductVO.getSupplierName())
		.address(shipmentBillVO.getAddress())
		.operator(shipmentBillVO.getOperator())
		.build();
		build.setIp(saveWaybillShipmentContext.getIp());
		build.setGmtCreatePin(saveWaybillShipmentContext.getPin());
		build.setGmtModifiedPin(saveWaybillShipmentContext.getPin());
		return build;
	}
	
	private  WaybillShipmentVolumeDO bulidWaybillShipmentVolume(WaybillVO waybillVO,int i,CalculateSupplierWeightVolumeContext calculateSupplierWeightVolumeContext,SaveWaybillShipmentContext saveWaybillShipmentContext){
		WaybillShipmentVolumeDO build = WaybillShipmentVolumeDO.builder()
		.shipmentVolumeCode(TmsSerializeConstant.WAYBILL_SHIPMENT_VOLUME_KEY + globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.WAYBILL_SHIPMENT_VOLUME_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY))
		.waybillCode(waybillVO.getWaybillCode())
		.companyCode(waybillVO.getCompanyCode())
		.supplierNo(waybillVO.getSupplierNo())
		.supplierSubNo(waybillVO.getSupplierNo()+"-"+i)
		.weight(calculateSupplierWeightVolumeContext.getWeight())
		.length(calculateSupplierWeightVolumeContext.getLength())
		.width(calculateSupplierWeightVolumeContext.getWidth())
		.height(calculateSupplierWeightVolumeContext.getHeight())
		.unitVolume(calculateSupplierWeightVolumeContext.getUnitVolume())
		.unitChargeWeight(calculateSupplierWeightVolumeContext.getUnitChargeWeight())
		.unitVolumeWeight(calculateSupplierWeightVolumeContext.getUnitVolumeWeight())
		.girth(calculateSupplierWeightVolumeContext.getGirth())
		.build();
		build.setIp(saveWaybillShipmentContext.getIp());
		build.setGmtCreatePin(saveWaybillShipmentContext.getPin());
		build.setGmtModifiedPin(saveWaybillShipmentContext.getPin());
		return build;
	}

}
